package com.xlhl.init.common;

import lombok.Data;

import java.io.Serializable;

/**
 * @author xlhl
 */
@Data
public class Result<T> implements Serializable {
    private Integer code;

    private T data;

    private String errorMessage;

    public static <T> Result<T> error(CustomHttpCode httpCode) {
        return error(httpCode.getCode(), httpCode.getMessage());
    }

    public static <T> Result<T> error(CustomHttpCode code, String errorMessage) {
        return error(code.getCode(), errorMessage);
    }

    public static <T> Result<T> error(String errorMessage) {
        return error(CustomHttpCode.SYSTEM_ERROR.getCode(), errorMessage);
    }

    public static <T> Result<T> error(Integer code, String errorMessage) {
        return info(code, null, errorMessage);
    }

    public static <T> Result<T> success() {
        return success(null);
    }

    public static <T> Result<T> success(T data) {
        return success(data, null);
    }

    public static <T> Result<T> success(T data, String errorMessage) {
        return info(CustomHttpCode.SUCCESS.getCode(), data, errorMessage);
    }

    public static <T> Result<T> info(Integer code, T data, String errorMessage) {
        return new Result<>(code, data, errorMessage);
    }

    public Result(Integer code, T data, String errorMessage) {
        this.code = code;
        this.data = data;
        this.errorMessage = errorMessage;
    }
}
